import Vue from "vue";
import axios from "axios";
// import store from "@/store";
// import router from "@/router";

import {
  API_ROOT
} from '@/config/env.js'


// 添加请求拦截器
axios.interceptors.request.use(
  function (req) {
    // 在发送请求之前做些什么
    let token = localStorage.getItem('token');
    if(token){
      req.headers['Authorization'] = 'Bearer '+token;
    }
    return req;
    // return res;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    let res = response.data;
    if(res.code == 401) { 
      // store.commit('user/logout')
      router.push({path: '/pagelogin'})
      // return Promise.reject(res);
    }
    // let {code, data} = res
    // debugger
    return res;
  },
  function (error) {

  }
);


/**
 * 
 * @param {*} option 
 * @returns 
 * 
 *   
 
  this.$api({
        url: "/service.php",
        method: "get",
        data: {
          action: "product_plist",
          name: "",
        },
      }).then((res) => {
        if (res.code == 200) {
          let data = res.data;
        }
      });


 */
function api(option) {
  let url = option.url
  let method = option.method
  let data = option.data || {}
  return;
  //
  method = method ? method.toLowerCase() : "get"; //请求方式
  data.userId = localStorage.getItem("userId") || "";
  data.token = localStorage.getItem("token") || "";
  //
  if (process.env.NODE_ENV !== "production") {
    url = API_ROOT + "/service.php";//
    // url =  "/api/" + url;//跨域时 使用代理
  } else {
    url = "/service.php";
  }

  let otherConfig = {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    transformRequest: [
      function (data) {
        let ret = "";
        let items = []
        for (let key in data) {
          let str = encodeURIComponent(key) + "=" + encodeURIComponent(data[key])
          items.push(str)
        }
        ret = items.join('&')
        return ret ;
      },
    ]
  };

  if (method == "get") {
    return axios({
      url: url,
      method: method,
      params: data,
      ...otherConfig,
    });

  } else if (method == "post") {
    return axios({
      url: url,
      method: method,
      data: data,
      ...otherConfig,
    });
  }
}

function httpApi(action, data, method, uploaderConfig) {
  
  let reqMethod = method ? method.toLowerCase() : "get"; //请求方式
  let url = action;

  if (process.env.NODE_ENV == "production") {
    url = "http://yaojiandan.api.dx.hdapp.com.cn" + action; // 正式环境
  }

  if (reqMethod == "get") {
    // return axios({
    //   url: reqUrl,
    //   method: reqMethod,
    //   params: reqData,
    //   ...otherConfig,
    //   ...uploaderConfig,
    // });

    // return axios.get(reqUrl, {
    //   params: reqData,
    // });
    return axios({
      url: url,
      method: reqMethod,
      params:  data ,
    });

  } else if (reqMethod == "post") {
    return axios({
      url: url,
      method: reqMethod,
      data:  data ,
    });
  }
}

Vue.prototype.$axios = axios;
Vue.prototype.$api = api;
Vue.prototype.$httpApi = httpApi;



export default {
  api,
  httpApi
};
